/**
 * esp source creater main.js
 * 
 * @NAmdConfig ./configuration.json
 * @NApiVersion 2.x
 */
define(
    ['N/record', 'N/error', 'N/search','N/runtime'
        ,'./so.builder'
        ,'./so.source.creater.factory'
    ],
    function(record, error,search,runtime,soBuilder,creater) {
        var attribute = new Object();
        var method = new Object();

        //入参  o2o的报文
        method.createSalesOrders = function(context){

        };

        //入参 source的context
        method.createSalesOrderBySourceContext = function(context){
            var platform = context.platform;
            return method.handleOneOrder(
                creater.method.getCreaterByPlatform(platform),
                context
            );
        };

        method.handleOneOrder = function(builder,order){
            log.debug({
                title: 'order',
                details: JSON.stringify(order)
            });
            var result = JSON.parse('{}');
            result['orderId'] = order.orderId;

                var afterHandleOrder = soBuilder.method.createAndSaveOrder(builder,order);
                if(afterHandleOrder.storeId){
                    result.storeId = afterHandleOrder.storeId;
                }
                if(afterHandleOrder.nsObjId){
                    result['nsObjId'] = afterHandleOrder.nsObjId;
                    result['success'] = true;
                    result['message'] = null;
                }else{
                    result['nsObjId'] = null;
                    result['success'] = false;
                    result['message'] = afterHandleOrder.errorMsg;
                }

            return result;
        }


		return {
            attribute:attribute,
            method:method
		};
	});